package com.woniuxy.shop.service;

import java.util.List;

import com.woniuxy.shop.dao.CartDao;
import com.woniuxy.shop.dao.GoodsDao;
import com.woniuxy.shop.dao.UserDao;
import com.woniuxy.shop.entity.Cart;
import com.woniuxy.shop.entity.Goods;
import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.exception.DaoException;
import com.woniuxy.shop.exception.NotExistGoodsException;
import com.woniuxy.shop.exception.NotExistUserException;
import com.woniuxy.shop.exception.ServiceException;

/**
*@author 作者：
*version 创建时间：
*类说明：
*/
public class CartService {
	CartDao cd = new CartDao();
	UserDao ud = new UserDao();
	GoodsDao gd = new GoodsDao();
	
	public void add(Cart c) {
		try {
			//查询用户ID是否存在，商品ID是否存在
			User u = ud.getUserByID(c.getUserid());
			Goods g = gd.getGoodsByID(c.getGoodsid());
			if(u==null) {
				throw new NotExistUserException("用户不存在");
			}
			if(g==null) {
				throw new NotExistGoodsException("商品不存在");
			}
			cd.add(c);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
	}

	/**
	 * 获取指定用户的购物车信息
	 * @param userid
	 * @return
	 */
	public List<Cart> getCartsByUser(int userid) {
		List<Cart> carts = null;
		try {
			carts = cd.getCartsByUser(userid);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		return carts;
	}

	public void del(int cartid) {
		try {
			cd.delByCart(cartid);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		
	}
}
